package code_analysis

import (
	"gitee.com/huanminabc/go-utils/common"
	"strconv"
	"strings"
)

// 清除多行代码中的前后空格
func ClearMultiRowSpace(lineCodes []string) {
	for i := 0; i < len(lineCodes); i++ {
		lineCodes[i] = strings.TrimSpace(lineCodes[i])
	}
}

// 提取{}中的内容,然后存储到map中,把{}内部的内容替换成占位符
func ContentBracketDisplace(lineCodes []string) map[string][]string {
	var mapStr = make(map[string][]string)
	var index = 0
	for ; index < len(lineCodes); index++ {
		//判断当前行结尾是否有{
		if strings.HasSuffix(lineCodes[index], "{") {
			var start = 1
			var uuid = "@ADDR@" + strconv.FormatInt(common.RandStringRunesIncrement(), 10) + "@ADDR@"
			//每次遇到{就+1,遇到}就-1,直到为0
			for i := index + 1; i < len(lineCodes); i++ {
				if strings.HasPrefix(lineCodes[i], "}") {
					start--
				}
				if start == 0 {
					//替换{}内容,把内容取出来
					var newLines []string
					for j := index + 1; j < i; j++ {
						newLines = append(newLines, lineCodes[j])
					}
					//删除{}内容
					lineCodes = append(lineCodes[:index+2], lineCodes[i:]...)
					//替换成占位符
					lineCodes[index+1] = uuid
					index = index + 1
					//存储{}内容
					mapStr[uuid] = newLines
					break
				}
				if strings.HasSuffix(lineCodes[i], "{") {
					start++
				}

			}

		}
	}
	return mapStr
}
